package com.siwei.eqi.util;

import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 *
 */
public class WeightedCalculatorUtil {

    public static BigDecimal areaWeightedCalculator(List<RegionIndex> regionIndexs){
        BigDecimal res = new BigDecimal(0);
        if (CollectionUtils.isEmpty(regionIndexs)){
            return null;
        }

        BigDecimal totalArea = regionIndexs.stream()
                .map(RegionIndex::getArea)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        for (RegionIndex regionIndex : regionIndexs){
                BigDecimal divide = regionIndex.getArea().divide(totalArea,12, RoundingMode.HALF_UP);
                BigDecimal multiply1 = divide.multiply(regionIndex.getIndexValue());
                res = res.add(multiply1);
        }
        return res;
    }

    @Data
    public static class RegionIndex{
        private BigDecimal area;
        private BigDecimal indexValue;
    }
}
